using System;
using System.Collections;
using ScenarioFramework.WorkflowEngine.DataModel;

namespace ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses
{
	//*********************************************************************
	//
	// XmlWorkflows Class
	//
	// The XmlWorkflows class represents a collection base of XmlWorkflow
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XmlWorkflows Has no fields
	//
	//*********************************************************************

	public class XMLWorkflows: bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE	= "WorkflowsProcesses";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLWorkflows()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLWorkflows(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		public override void Init()
		{
			this.RemoveAll();
		}

		/// <summary>
		/// Removes the process from list.
		/// </summary>
		/// <param name="sWorkflowProcessName">Name of the WorkflowProcess to remove.</param>
		/// <returns>true process removed else false.</returns>
		public bool RemoveProcess(string sWorkflowProcessName)
		{
			XMLWorkflow xmlWorkflow = null;
			for ( int i=0; i<this.Count;i++)
			{
				xmlWorkflow = (XMLWorkflow)this[i];

				if ( xmlWorkflow.Name == sWorkflowProcessName)
				{
					this.RemoveAt (i);
					return true;
				}
			}

			return false;
		}

		public XMLWorkflow GetWorkflowByName(string sWorkflowProcessName)
		{
			XMLWorkflow xmlWorkflow = null;
			for ( int i=0; i<this.Count;i++)
			{
				xmlWorkflow = (XMLWorkflow)this[i];

				if ( xmlWorkflow.Name == sWorkflowProcessName)
				{
					return xmlWorkflow;
				}
			}

			return null;
		}

		public override void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLWorkflow.MAIN_NODE:
						this.Add ( new XMLWorkflow(node));
						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLWorkflows.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );

			}

			return xmlNode;
		}

		/** Instance methods to check if a WF is in collection **/

		/** TODO 
		almost in
		**/

		/** WAPI Wrappers to call SCENARIO ENGINE **/

		/** TODO 
		Create Workflow
		Delete Workflow
		Read Form Configuration
		**/

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name,
			Description
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Description:
					InnerList.Sort(new DescriptionComparer());
					break;
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLWorkflow first = (XMLWorkflow) x;
				XMLWorkflow second = (XMLWorkflow) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class DescriptionComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLWorkflow first = (XMLWorkflow) x;
				XMLWorkflow second = (XMLWorkflow) y;
				return first.Description.CompareTo(second.Description);
			}
		}

		
	}
}